package groupb6.shopsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import groupb6.shopsystem.mapper.DestinationMapper;
import groupb6.shopsystem.mapper.UserMapper;
import groupb6.shopsystem.pojo.entity.Destination;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.pojo.form.AddAddressForm;
import groupb6.shopsystem.pojo.form.CustomerEditForm;
import groupb6.shopsystem.pojo.form.UpdateAddressForm;
import groupb6.shopsystem.service.CustomerInfoService;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.enums.IsDefaultDestEnum;
import groupb6.shopsystem.util.enums.ResponseEnum;
import groupb6.shopsystem.util.response.ResponseException;
import groupb6.shopsystem.util.response.UniversalResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public UniversalResponse<List<Destination>> getDestinations() {
        User user = JWTUtil.getCurrentUser();
        List<Destination> destinations = destinationMapper.getByUser_id(user.getId());
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), destinations);
    }

    @Override
    public UniversalResponse<?> updateUserDestination(UpdateAddressForm updateAddressForm) {
        if (updateAddressForm.getId() == null) {
            throw new ResponseException(ResponseEnum.PARAM_IS_BLANK.getCode(), ResponseEnum.PARAM_IS_BLANK.getMsg());
        }
        destinationMapper.updateDestById(updateAddressForm);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @Override
    public UniversalResponse<?> deleteUserDestination(Integer id) {
        if (id == null) {
            throw new ResponseException(ResponseEnum.PARAM_IS_BLANK.getCode(), ResponseEnum.PARAM_IS_BLANK.getMsg());
        }
        destinationMapper.deleteDestById(id);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @Override
    public UniversalResponse<?> insertUserDestination(AddAddressForm addAddressForm) {
        Destination newDestination = new Destination();
        newDestination.setId(null);
        newDestination.setUserId(JWTUtil.getCurrentUser().getId());
        newDestination.setName(addAddressForm.getName());
        newDestination.setAddress(addAddressForm.getAddress());
        newDestination.setPhone(addAddressForm.getPhone());
        newDestination.setIsDefault(IsDefaultDestEnum.NOT_DEFAULT.getValue());

        destinationMapper.insert(newDestination);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @Override
    public UniversalResponse<?> changeDefaultDestination(Integer destination_id) {
        Integer userId = JWTUtil.getCurrentUser().getId();
        destinationMapper.changeDefaultOtherDestByUserId(userId);
        destinationMapper.changeDefaultDestById(destination_id);
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @Override
    public User getInfo() {
        userMapper.selectById(JWTUtil.getCurrentUser().getId());
        return userMapper.selectById(JWTUtil.getCurrentUser().getId());
    }

    @Override
    public void editInfo(CustomerEditForm form) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User user = User.builder()
                .avatar(form.getAvatar())
                .tel(form.getTel())
                .name(form.getName()).password(form.getPassword()).
                build();

        wrapper.eq("id", JWTUtil.getCurrentUser().getId());
        userMapper.update(user, wrapper);
    }
}
